home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / app / gimpcontext.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-12  |  43.4 KB  |  1,739 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * gimpcontext.c: Copyright (C) 1999 Michael Natterer <mitch@gimp.org>
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  */
  20.  
  21. #include "config.h"
  22.  
  23. #include <gtk/gtk.h>
  24.  
  25. #include "apptypes.h"
  26.  
  27. #include "appenv.h"
  28. #include "gimpbrush.h"
  29. #include "gimpbrushlist.h"
  30. #include "gimpcontext.h"
  31. #include "gimprc.h"
  32. #include "gimpsignal.h"
  33. #include "gradient_header.h"
  34. #include "patterns.h"
  35.  
  36. #define context_return_if_fail(context) \
  37.         g_return_if_fail ((context) != NULL); \
  38.         g_return_if_fail (GIMP_IS_CONTEXT (context))
  39.  
  40. #define context_return_val_if_fail(context,val) \
  41.         g_return_val_if_fail ((context) != NULL, (val)); \
  42.         g_return_val_if_fail (GIMP_IS_CONTEXT (context), (val))
  43.  
  44. #define context_check_current(context) \
  45.         ((context) = (context) ? (context) : current_context)
  46.  
  47. #define context_find_defined(context,arg_mask) \
  48.         while (!(((context)->defined_args) & arg_mask) && (context)->parent) \
  49.           (context) = (context)->parent
  50.  
  51. typedef void (* GimpContextCopyArgFunc) (GimpContext *src, GimpContext *dest);
  52.  
  53. /*  local function prototypes  */
  54.  
  55. /*  image  */
  56. static void gimp_context_real_set_image      (GimpContext      *context,
  57.                           GimpImage        *image);
  58. static void gimp_context_copy_image          (GimpContext      *src,
  59.                           GimpContext      *dest);
  60.  
  61. /*  display  */
  62. static void gimp_context_real_set_display    (GimpContext      *context,
  63.                           GDisplay         *display);
  64. static void gimp_context_copy_display        (GimpContext      *src,
  65.                           GimpContext      *dest);
  66.  
  67. /*  tool  */
  68. static void gimp_context_real_set_tool       (GimpContext      *context,
  69.                           ToolType          tool);
  70. static void gimp_context_copy_tool           (GimpContext      *src,
  71.                           GimpContext      *dest);
  72.  
  73. /*  foreground  */
  74. static void gimp_context_real_set_foreground (GimpContext      *context,
  75.                           gint              r,
  76.                           gint              g,
  77.                           gint              b);
  78. static void gimp_context_copy_foreground     (GimpContext      *src,
  79.                           GimpContext      *dest);
  80.  
  81. /*  background  */
  82. static void gimp_context_real_set_background (GimpContext      *context,
  83.                           gint              r,
  84.                           gint              g,
  85.                           gint              b);
  86. static void gimp_context_copy_background     (GimpContext      *src,
  87.                           GimpContext      *dest);
  88.  
  89. /*  opacity  */
  90. static void gimp_context_real_set_opacity    (GimpContext      *context,
  91.                           gdouble           opacity);
  92. static void gimp_context_copy_opacity        (GimpContext      *src,
  93.                           GimpContext      *dest);
  94.  
  95. /*  paint mode  */
  96. static void gimp_context_real_set_paint_mode (GimpContext      *context,
  97.                           LayerModeEffects  paint_mode);
  98. static void gimp_context_copy_paint_mode     (GimpContext      *src,
  99.                           GimpContext      *dest);
  100.  
  101. /*  brush  */
  102. static void gimp_context_real_set_brush      (GimpContext      *context,
  103.                           GimpBrush        *brush);
  104. static void gimp_context_copy_brush          (GimpContext      *src,
  105.                           GimpContext      *dest);
  106.  
  107. /*  pattern  */
  108. static void gimp_context_real_set_pattern    (GimpContext      *context,
  109.                           GPattern         *pattern);
  110. static void gimp_context_copy_pattern        (GimpContext      *src,
  111.                           GimpContext      *dest);
  112.  
  113. /*  gradient  */
  114. static void gimp_context_real_set_gradient   (GimpContext      *context,
  115.                           gradient_t       *gradient);
  116. static void gimp_context_copy_gradient       (GimpContext      *src,
  117.                           GimpContext      *dest);
  118.  
  119. /*  arguments  */
  120.  
  121. enum
  122. {
  123.   ARG_0,
  124.   ARG_IMAGE,
  125.   ARG_DISPLAY,
  126.   ARG_TOOL,
  127.   ARG_FOREGROUND,
  128.   ARG_BACKGROUND,
  129.   ARG_OPACITY,
  130.   ARG_PAINT_MODE,
  131.   ARG_BRUSH,
  132.   ARG_PATTERN,
  133.   ARG_GRADIENT
  134. };
  135.  
  136. static gchar *gimp_context_arg_names[] =
  137. {
  138.   "GimpContext::image",
  139.   "GimpContext::display",
  140.   "GimpContext::tool",
  141.   "GimpContext::foreground",
  142.   "GimpContext::background",
  143.   "GimpContext::opacity",
  144.   "GimpContext::paint_mode",
  145.   "GimpContext::brush",
  146.   "GimpContext::pattern",
  147.   "GimpContext::gradient"
  148. };
  149.  
  150. static GimpContextCopyArgFunc gimp_context_copy_arg_funcs[] =
  151. {
  152.   gimp_context_copy_image,
  153.   gimp_context_copy_display,
  154.   gimp_context_copy_tool,
  155.   gimp_context_copy_foreground,
  156.   gimp_context_copy_background,
  157.   gimp_context_copy_opacity,
  158.   gimp_context_copy_paint_mode,
  159.   gimp_context_copy_brush,
  160.   gimp_context_copy_pattern,
  161.   gimp_context_copy_gradient
  162. };
  163.  
  164. /*  signals  */
  165.  
  166. enum
  167. {
  168.   IMAGE_CHANGED,
  169.   DISPLAY_CHANGED,
  170.   TOOL_CHANGED,
  171.   FOREGROUND_CHANGED,
  172.   BACKGROUND_CHANGED,
  173.   OPACITY_CHANGED,
  174.   PAINT_MODE_CHANGED,
  175.   BRUSH_CHANGED,
  176.   PATTERN_CHANGED,
  177.   GRADIENT_CHANGED,
  178.   LAST_SIGNAL
  179. };
  180.  
  181. static guint gimp_context_signals[LAST_SIGNAL] = { 0 };
  182.  
  183. static gchar *gimp_context_signal_names[] =
  184. {
  185.   "image_changed",
  186.   "display_changed",
  187.   "tool_changed",
  188.   "foreground_changed",
  189.   "background_changed",
  190.   "opacity_changed",
  191.   "paint_mode_changed",
  192.   "brush_changed",
  193.   "pattern_changed",
  194.   "gradient_changed"
  195. };
  196.  
  197. static GtkSignalFunc gimp_context_signal_handlers[] =
  198. {
  199.   gimp_context_real_set_image,
  200.   gimp_context_real_set_display,
  201.   gimp_context_real_set_tool,
  202.   gimp_context_real_set_foreground,
  203.   gimp_context_real_set_background,
  204.   gimp_context_real_set_opacity,
  205.   gimp_context_real_set_paint_mode,
  206.   gimp_context_real_set_brush,
  207.   gimp_context_real_set_pattern,
  208.   gimp_context_real_set_gradient
  209. };
  210.  
  211. static GimpObjectClass * parent_class = NULL;
  212.  
  213. /*  the currently active context  */
  214. static GimpContext *current_context  = NULL;
  215.  
  216. /*  the context user by the interface  */
  217. static GimpContext *user_context     = NULL;
  218.  
  219. /*  the default context which is initialized from gimprc  */
  220. static GimpContext *default_context  = NULL;
  221.  
  222. /*  the hardcoded standard context  */
  223. static GimpContext *standard_context = NULL;
  224.  
  225. /*  the list of all contexts  */
  226. static GSList      *context_list     = NULL;
  227.  
  228. /*****************************************************************************/
  229. /*  private functions  *******************************************************/
  230.  
  231. static void
  232. gimp_context_set_arg (GtkObject *object,
  233.               GtkArg    *arg,
  234.               guint      arg_id)
  235. {
  236.   GimpContext *context;
  237.  
  238.   context = GIMP_CONTEXT (object);
  239.  
  240.   switch (arg_id)
  241.     {
  242.     case ARG_IMAGE:
  243.       gimp_context_set_image (context, GTK_VALUE_POINTER (*arg));
  244.       break;
  245.     case ARG_DISPLAY:
  246.       gimp_context_set_display (context, GTK_VALUE_POINTER (*arg));
  247.       break;
  248.     case ARG_TOOL:
  249.       gimp_context_set_tool (context, GTK_VALUE_INT (*arg));
  250.       break;
  251.     case ARG_FOREGROUND:
  252.       {
  253.     guchar *col = GTK_VALUE_POINTER (*arg);
  254.     gimp_context_set_foreground (context, col[0], col[1], col[2]);
  255.       }
  256.       break;
  257.     case ARG_BACKGROUND:
  258.       {
  259.     guchar *col = GTK_VALUE_POINTER (*arg);
  260.     gimp_context_set_background (context, col[0], col[1], col[2]);
  261.       }
  262.       break;
  263.     case ARG_OPACITY:
  264.       gimp_context_set_opacity (context, GTK_VALUE_DOUBLE (*arg));
  265.       break;
  266.     case ARG_PAINT_MODE:
  267.       gimp_context_set_paint_mode (context, GTK_VALUE_INT (*arg));
  268.       break;
  269.     case ARG_BRUSH:
  270.       gimp_context_set_brush (context, GTK_VALUE_POINTER (*arg));
  271.       break;
  272.     case ARG_PATTERN:
  273.       gimp_context_set_pattern (context, GTK_VALUE_POINTER (*arg));
  274.       break;
  275.     case ARG_GRADIENT:
  276.       gimp_context_set_gradient (context, GTK_VALUE_POINTER (*arg));
  277.       break;
  278.     default:
  279.       break;
  280.     }
  281. }
  282.  
  283. static void
  284. gimp_context_get_arg (GtkObject *object,
  285.               GtkArg    *arg,
  286.               guint      arg_id)
  287. {
  288.   GimpContext *context;
  289.  
  290.   context = GIMP_CONTEXT (object);
  291.  
  292.   switch (arg_id)
  293.     {
  294.     case ARG_IMAGE:
  295.       GTK_VALUE_POINTER (*arg) = gimp_context_get_image (context);
  296.       break;
  297.     case ARG_DISPLAY:
  298.       GTK_VALUE_POINTER (*arg) = gimp_context_get_display (context);
  299.       break;
  300.     case ARG_TOOL:
  301.       GTK_VALUE_INT (*arg) = gimp_context_get_tool (context);
  302.       break;
  303.     case ARG_FOREGROUND:
  304.       {
  305.     guchar *col = GTK_VALUE_POINTER (*arg);
  306.     gimp_context_get_foreground (context, &col[0], &col[1], &col[2]);
  307.       }
  308.       break;
  309.     case ARG_BACKGROUND:
  310.       {
  311.     guchar *col = GTK_VALUE_POINTER (*arg);
  312.     gimp_context_get_background (context, &col[0], &col[1], &col[2]);
  313.       }
  314.       break;
  315.     case ARG_OPACITY:
  316.       GTK_VALUE_DOUBLE (*arg) = gimp_context_get_opacity (context);
  317.       break;
  318.     case ARG_PAINT_MODE:
  319.       GTK_VALUE_INT (*arg) = gimp_context_get_paint_mode (context);
  320.       break;
  321.     case ARG_BRUSH:
  322.       GTK_VALUE_POINTER (*arg) = gimp_context_get_brush (context);
  323.       break;
  324.     case ARG_PATTERN:
  325.       GTK_VALUE_POINTER (*arg) = gimp_context_get_pattern (context);
  326.       break;
  327.     case ARG_GRADIENT:
  328.       GTK_VALUE_POINTER (*arg) = gimp_context_get_gradient (context);
  329.       break;
  330.     default:
  331.       arg->type = GTK_TYPE_INVALID;
  332.       break;
  333.     }
  334. }
  335.  
  336. static void
  337. gimp_context_destroy (GtkObject *object)
  338. {
  339.   GimpContext *context;
  340.  
  341.   context = GIMP_CONTEXT (object);
  342.  
  343.   if (context->parent)
  344.     gimp_context_unset_parent (context);
  345.  
  346.   if (context->name)
  347.     {
  348.       g_free (context->name);
  349.       context->name = NULL;
  350.     }
  351.  
  352.   if (context->image)
  353.     gtk_signal_disconnect_by_data (GTK_OBJECT (image_context), context);
  354.  
  355.   if (context->display)
  356.     gtk_signal_disconnect_by_data (GTK_OBJECT (context->display->shell),
  357.                    context);
  358.  
  359.   if (context->brush)
  360.     {
  361.       gtk_signal_disconnect_by_data (GTK_OBJECT (context->brush), context);
  362.       gtk_signal_disconnect_by_data (GTK_OBJECT (brush_list), context);
  363.     }
  364.  
  365.   if (context->brush_name)
  366.     {
  367.       g_free (context->brush_name);
  368.       context->brush_name = NULL;
  369.     }
  370.  
  371.   if (context->pattern_name)
  372.     {
  373.       g_free (context->pattern_name);
  374.       context->pattern_name = NULL;
  375.     }
  376.  
  377.   if (context->gradient_name)
  378.     {
  379.       g_free (context->gradient_name);
  380.       context->gradient_name = NULL;
  381.     }
  382.  
  383.   if (GTK_OBJECT_CLASS (parent_class)->destroy)
  384.     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
  385.  
  386.   context_list = g_slist_remove (context_list, context);
  387. }
  388.  
  389. static void
  390. gimp_context_class_init (GimpContextClass *klass)
  391. {
  392.   GtkObjectClass *object_class;
  393.  
  394.   object_class = GTK_OBJECT_CLASS (klass);
  395.  
  396.   gtk_object_add_arg_type (gimp_context_arg_names[IMAGE_CHANGED],
  397.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_IMAGE);
  398.   gtk_object_add_arg_type (gimp_context_arg_names[DISPLAY_CHANGED],
  399.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_DISPLAY);
  400.   gtk_object_add_arg_type (gimp_context_arg_names[TOOL_CHANGED],
  401.                GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_TOOL);
  402.   gtk_object_add_arg_type (gimp_context_arg_names[FOREGROUND_CHANGED],
  403.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_FOREGROUND);
  404.   gtk_object_add_arg_type (gimp_context_arg_names[BACKGROUND_CHANGED],
  405.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_BACKGROUND);
  406.   gtk_object_add_arg_type (gimp_context_arg_names[OPACITY_CHANGED],
  407.                GTK_TYPE_DOUBLE, GTK_ARG_READWRITE, ARG_OPACITY);
  408.   gtk_object_add_arg_type (gimp_context_arg_names[PAINT_MODE_CHANGED],
  409.                GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_PAINT_MODE);
  410.   gtk_object_add_arg_type (gimp_context_arg_names[BRUSH_CHANGED],
  411.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_BRUSH);
  412.   gtk_object_add_arg_type (gimp_context_arg_names[PATTERN_CHANGED],
  413.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_PATTERN);
  414.   gtk_object_add_arg_type (gimp_context_arg_names[GRADIENT_CHANGED],
  415.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_GRADIENT);
  416.  
  417.   parent_class = gtk_type_class (gimp_object_get_type ());
  418.  
  419.   gimp_context_signals[IMAGE_CHANGED] =
  420.     gimp_signal_new (gimp_context_signal_names[IMAGE_CHANGED],
  421.              GTK_RUN_FIRST,
  422.              object_class->type,
  423.              GTK_SIGNAL_OFFSET (GimpContextClass,
  424.                     image_changed),
  425.              gimp_sigtype_pointer);
  426.  
  427.   gimp_context_signals[DISPLAY_CHANGED] =
  428.     gimp_signal_new (gimp_context_signal_names[DISPLAY_CHANGED],
  429.              GTK_RUN_FIRST,
  430.              object_class->type,
  431.              GTK_SIGNAL_OFFSET (GimpContextClass,
  432.                     display_changed),
  433.              gimp_sigtype_pointer);
  434.  
  435.   gimp_context_signals[TOOL_CHANGED] =
  436.     gimp_signal_new (gimp_context_signal_names[TOOL_CHANGED],
  437.              GTK_RUN_FIRST,
  438.              object_class->type,
  439.              GTK_SIGNAL_OFFSET (GimpContextClass,
  440.                     tool_changed),
  441.              gimp_sigtype_int);
  442.  
  443.   gimp_context_signals[FOREGROUND_CHANGED] =
  444.     gimp_signal_new (gimp_context_signal_names[FOREGROUND_CHANGED],
  445.              GTK_RUN_FIRST,
  446.              object_class->type,
  447.              GTK_SIGNAL_OFFSET (GimpContextClass,
  448.                     foreground_changed),
  449.              gimp_sigtype_int_int_int);
  450.  
  451.   gimp_context_signals[BACKGROUND_CHANGED] =
  452.     gimp_signal_new (gimp_context_signal_names[BACKGROUND_CHANGED],
  453.              GTK_RUN_FIRST,
  454.              object_class->type,
  455.              GTK_SIGNAL_OFFSET (GimpContextClass,
  456.                     background_changed),
  457.              gimp_sigtype_int_int_int);
  458.  
  459.   gimp_context_signals[OPACITY_CHANGED] =
  460.     gimp_signal_new (gimp_context_signal_names[OPACITY_CHANGED],
  461.              GTK_RUN_FIRST,
  462.              object_class->type,
  463.              GTK_SIGNAL_OFFSET (GimpContextClass,
  464.                     opacity_changed),
  465.              gimp_sigtype_double);
  466.  
  467.   gimp_context_signals[PAINT_MODE_CHANGED] =
  468.     gimp_signal_new (gimp_context_signal_names[PAINT_MODE_CHANGED],
  469.              GTK_RUN_FIRST,
  470.              object_class->type,
  471.              GTK_SIGNAL_OFFSET (GimpContextClass,
  472.                     paint_mode_changed),
  473.              gimp_sigtype_int);
  474.  
  475.   gimp_context_signals[BRUSH_CHANGED] =
  476.     gimp_signal_new (gimp_context_signal_names[BRUSH_CHANGED],
  477.              GTK_RUN_FIRST,
  478.              object_class->type,
  479.              GTK_SIGNAL_OFFSET (GimpContextClass,
  480.                     brush_changed),
  481.              gimp_sigtype_pointer);
  482.  
  483.   gimp_context_signals[PATTERN_CHANGED] =
  484.     gimp_signal_new (gimp_context_signal_names[PATTERN_CHANGED],
  485.              GTK_RUN_FIRST,
  486.              object_class->type,
  487.              GTK_SIGNAL_OFFSET (GimpContextClass,
  488.                     pattern_changed),
  489.              gimp_sigtype_pointer);
  490.  
  491.   gimp_context_signals[GRADIENT_CHANGED] =
  492.     gimp_signal_new (gimp_context_signal_names[GRADIENT_CHANGED],
  493.              GTK_RUN_FIRST,
  494.              object_class->type,
  495.              GTK_SIGNAL_OFFSET (GimpContextClass,
  496.                     gradient_changed),
  497.              gimp_sigtype_pointer);
  498.  
  499.   gtk_object_class_add_signals (object_class, gimp_context_signals,
  500.                 LAST_SIGNAL);
  501.  
  502.   object_class->set_arg = gimp_context_set_arg;
  503.   object_class->get_arg = gimp_context_get_arg;
  504.   object_class->destroy = gimp_context_destroy;
  505.  
  506.   klass->image_changed      = NULL;
  507.   klass->display_changed    = NULL;
  508.   klass->tool_changed       = NULL;
  509.   klass->foreground_changed = NULL;
  510.   klass->background_changed = NULL;
  511.   klass->opacity_changed    = NULL;
  512.   klass->paint_mode_changed = NULL;
  513.   klass->brush_changed      = NULL;
  514.   klass->pattern_changed    = NULL;
  515.   klass->gradient_changed   = NULL;
  516. }
  517.  
  518. static void
  519. gimp_context_init (GimpContext *context)
  520. {
  521.   context->name   = NULL;
  522.   context->parent = NULL;
  523.  
  524.   context->defined_args = GIMP_CONTEXT_ALL_ARGS_MASK;
  525.  
  526.   context->image   = NULL;
  527.   context->display = NULL;
  528.  
  529.   context->tool = RECT_SELECT;
  530.  
  531.   context->foreground[0] = 0;
  532.   context->foreground[1] = 0;
  533.   context->foreground[2] = 0;
  534.  
  535.   context->background[0] = 255;
  536.   context->background[1] = 255;
  537.   context->background[2] = 255;
  538.  
  539.   context->opacity    = 1.0;
  540.   context->paint_mode = NORMAL_MODE;
  541.  
  542.   context->brush      = NULL;
  543.   context->brush_name = NULL;
  544.  
  545.   context->pattern      = NULL;
  546.   context->pattern_name = NULL;
  547.  
  548.   context->gradient      = NULL;
  549.   context->gradient_name = NULL;
  550.  
  551.   context_list = g_slist_prepend (context_list, context);
  552. }
  553.  
  554. /*****************************************************************************/
  555. /*  public functions  ********************************************************/
  556.  
  557. GtkType
  558. gimp_context_get_type (void)
  559. {
  560.   static GtkType context_type = 0;
  561.  
  562.   if (! context_type)
  563.     {
  564.       GtkTypeInfo context_info =
  565.       {
  566.     "GimpContext",
  567.     sizeof (GimpContext),
  568.     sizeof (GimpContextClass),
  569.     (GtkClassInitFunc) gimp_context_class_init,
  570.     (GtkObjectInitFunc) gimp_context_init,
  571.     /* reserved_1 */ NULL,
  572.     /* reserved_2 */ NULL,
  573.     (GtkClassInitFunc) NULL
  574.       };
  575.  
  576.       context_type = gtk_type_unique (gimp_object_get_type (), &context_info);
  577.     }
  578.  
  579.   return context_type;
  580. }
  581.  
  582. GimpContext *
  583. gimp_context_new (const gchar *name,
  584.           GimpContext *template)
  585. {
  586.   GimpContext *context;
  587.  
  588.   g_return_val_if_fail (!template || GIMP_IS_CONTEXT (template), NULL);
  589.  
  590.   context = gtk_type_new (gimp_context_get_type ());
  591.  
  592.   /*  FIXME: need unique names here  */
  593.   context->name = g_strdup (name ? name : "Unnamed");
  594.  
  595.   if (template)
  596.     {
  597.       context->defined_args = template->defined_args;
  598.  
  599.       gimp_context_copy_args (template, context, GIMP_CONTEXT_ALL_ARGS_MASK);
  600.     }
  601.  
  602.   return context;
  603. }
  604.  
  605. /*****************************************************************************/
  606. /*  getting/setting the special contexts  ************************************/
  607.  
  608. GimpContext *
  609. gimp_context_get_current (void)
  610. {
  611.   return current_context;
  612. }
  613.  
  614. void
  615. gimp_context_set_current (GimpContext *context)
  616. {
  617.   current_context = context;
  618. }
  619.  
  620. GimpContext *
  621. gimp_context_get_user (void)
  622. {
  623.   return user_context;
  624. }
  625.  
  626. void
  627. gimp_context_set_user (GimpContext *context)
  628. {
  629.   user_context = context;
  630. }
  631.  
  632. GimpContext *
  633. gimp_context_get_default (void)
  634. {
  635.   return default_context;
  636. }
  637.  
  638. void
  639. gimp_context_set_default (GimpContext *context)
  640. {
  641.   default_context = context;
  642. }
  643.  
  644. GimpContext *
  645. gimp_context_get_standard (void)
  646. {
  647.   if (! standard_context)
  648.     standard_context = gimp_context_new ("Standard", NULL);
  649.  
  650.   return standard_context;
  651. }
  652.  
  653. /*****************************************************************************/
  654. /*  functions manipulating a single context  *********************************/
  655.  
  656. gchar *
  657. gimp_context_get_name (GimpContext *context)
  658. {
  659.   context_check_current (context);
  660.   context_return_val_if_fail (context, NULL);
  661.  
  662.   return context->name;
  663. }
  664.  
  665. void
  666. gimp_context_set_name (GimpContext *context,
  667.                const gchar *name)
  668. {
  669.   context_check_current (context);
  670.   context_return_if_fail (context);
  671.  
  672.   if (context->name)
  673.     g_free (context->name);
  674.  
  675.   context->name = g_strdup (name ? name : "Unnamed");
  676. }
  677.  
  678. GimpContext *
  679. gimp_context_get_parent (GimpContext *context)
  680. {
  681.   context_check_current (context);
  682.   context_return_val_if_fail (context, NULL);
  683.  
  684.   return context->parent;
  685. }
  686.  
  687. void
  688. gimp_context_set_parent (GimpContext *context,
  689.              GimpContext *parent)
  690. {
  691.   GimpContextArgType arg;
  692.  
  693.   context_check_current (context);
  694.   context_return_if_fail (context);
  695.   g_return_if_fail (!parent || GIMP_IS_CONTEXT (parent));
  696.  
  697.   if (context == parent)
  698.     return;
  699.  
  700.   for (arg = 0; arg < GIMP_CONTEXT_NUM_ARGS; arg++)
  701.     if (! ((1 << arg) & context->defined_args))
  702.       {
  703.     gimp_context_copy_arg (parent, context, arg);
  704.     gtk_signal_connect_object (GTK_OBJECT (parent),
  705.                    gimp_context_signal_names[arg],
  706.                    gimp_context_signal_handlers[arg],
  707.                    GTK_OBJECT (context));
  708.       }
  709.  
  710.   context->parent = parent;
  711. }
  712.  
  713. void
  714. gimp_context_unset_parent (GimpContext *context)
  715. {
  716.   context_check_current (context);
  717.   context_return_if_fail (context);
  718.  
  719.   if (context->parent)
  720.     {
  721.       if (context->defined_args != GIMP_CONTEXT_ALL_ARGS_MASK)
  722.     gtk_signal_disconnect_by_data (GTK_OBJECT (context->parent), context);
  723.  
  724.       context->parent = NULL;
  725.     }
  726. }
  727.  
  728. /*  define / undefinine context arguments  */
  729.  
  730. void
  731. gimp_context_define_arg (GimpContext        *context,
  732.              GimpContextArgType  arg,
  733.              gboolean            defined)
  734. {
  735.   GimpContextArgMask mask;
  736.  
  737.   context_check_current (context);
  738.   context_return_if_fail (context);
  739.   g_return_if_fail ((arg >= 0) && (arg < GIMP_CONTEXT_NUM_ARGS));
  740.  
  741.   mask = (1 << arg);
  742.  
  743.   if (defined)
  744.     {
  745.       if (! (context->defined_args & mask))
  746.     {
  747.       context->defined_args |= mask;
  748.       if (context->parent)
  749.         gtk_signal_disconnect_by_func (GTK_OBJECT (context->parent),
  750.                        gimp_context_signal_handlers[arg],
  751.                        context);
  752.     }
  753.     }
  754.   else
  755.     {
  756.       if (context->defined_args & mask)
  757.     {
  758.       context->defined_args &= ~mask;
  759.       if (context->parent)
  760.         {
  761.           gimp_context_copy_arg (context->parent, context, arg);
  762.           gtk_signal_connect_object (GTK_OBJECT (context->parent),
  763.                      gimp_context_signal_names[arg],
  764.                      gimp_context_signal_handlers[arg],
  765.                      GTK_OBJECT (context));
  766.         }
  767.     }
  768.     }
  769. }
  770.  
  771. gboolean
  772. gimp_context_arg_defined (GimpContext        *context,
  773.               GimpContextArgType  arg)
  774. {
  775.   context_check_current (context);
  776.   context_return_val_if_fail (context, FALSE);
  777.  
  778.   return (context->defined_args & (1 << arg)) ? TRUE : FALSE;
  779. }
  780.  
  781. void
  782. gimp_context_define_args (GimpContext        *context,
  783.               GimpContextArgMask  args_mask,
  784.               gboolean            defined)
  785. {
  786.   GimpContextArgType arg;
  787.  
  788.   context_check_current (context);
  789.   context_return_if_fail (context);
  790.  
  791.   for (arg = 0; arg < GIMP_CONTEXT_NUM_ARGS; arg++)
  792.     if ((1 << arg) & args_mask)
  793.       gimp_context_define_arg (context, arg, defined);
  794. }
  795.  
  796. /*  copying context arguments  */
  797.  
  798. void
  799. gimp_context_copy_arg (GimpContext        *src,
  800.                GimpContext        *dest,
  801.                GimpContextArgType  arg)
  802. {
  803.   context_check_current (src);
  804.   context_return_if_fail (src);
  805.   context_check_current (dest);
  806.   context_return_if_fail (dest);
  807.   g_return_if_fail ((arg >= 0) && (arg < GIMP_CONTEXT_NUM_ARGS));
  808.  
  809.   (* gimp_context_copy_arg_funcs[arg]) (src, dest);
  810. }
  811.  
  812. void
  813. gimp_context_copy_args (GimpContext        *src,
  814.             GimpContext        *dest,
  815.             GimpContextArgMask  args_mask)
  816. {
  817.   GimpContextArgType arg;
  818.  
  819.   context_check_current (src);
  820.   context_return_if_fail (src);
  821.   context_check_current (dest);
  822.   context_return_if_fail (dest);
  823.  
  824.   for (arg = 0; arg < GIMP_CONTEXT_NUM_ARGS; arg++)
  825.     if ((1 << arg) & args_mask)
  826.       {
  827.     gimp_context_copy_arg (src, dest, arg);
  828.       }
  829. }
  830.  
  831. /*  attribute access functions  */
  832.  
  833. /*****************************************************************************/
  834. /*  image  *******************************************************************/
  835.  
  836. GimpImage *
  837. gimp_context_get_image (GimpContext *context)
  838. {
  839.   context_check_current (context);
  840.   context_return_val_if_fail (context, NULL);
  841.  
  842.   return context->image;
  843. }
  844.  
  845. void
  846. gimp_context_set_image (GimpContext *context,
  847.             GimpImage   *image)
  848. {
  849.   context_check_current (context);
  850.   context_return_if_fail (context);
  851.   context_find_defined (context, GIMP_CONTEXT_IMAGE_MASK);
  852.  
  853.   gimp_context_real_set_image (context, image);
  854. }
  855.  
  856. void
  857. gimp_context_image_changed (GimpContext *context)
  858. {
  859.   context_check_current (context);
  860.   context_return_if_fail (context);
  861.  
  862.   gtk_signal_emit (GTK_OBJECT (context),
  863.            gimp_context_signals[IMAGE_CHANGED],
  864.            context->image);
  865. }
  866.  
  867. /*  handle disappearing images  */
  868. static void
  869. gimp_context_image_removed (GimpSet     *set,
  870.                 GimpImage   *image,
  871.                 GimpContext *context)
  872. {
  873.   if (context->image == image)
  874.     gimp_context_real_set_image (context, NULL);
  875. }
  876.  
  877. static void
  878. gimp_context_real_set_image (GimpContext *context,
  879.                  GimpImage   *image)
  880. {
  881.   if (context->image == image)
  882.     return;
  883.  
  884.   if (image == NULL)
  885.     gtk_signal_disconnect_by_data (GTK_OBJECT (image_context), context);
  886.  
  887.   if (context->image == NULL)
  888.     gtk_signal_connect (GTK_OBJECT (image_context), "remove",
  889.             GTK_SIGNAL_FUNC (gimp_context_image_removed),
  890.             context);
  891.  
  892.   context->image = image;
  893.   gimp_context_image_changed (context);
  894. }
  895.  
  896. static void
  897. gimp_context_copy_image (GimpContext *src,
  898.              GimpContext *dest)
  899. {
  900.   gimp_context_real_set_image (dest, src->image);
  901. }
  902.  
  903. /*****************************************************************************/
  904. /*  display  *****************************************************************/
  905.  
  906. GDisplay *
  907. gimp_context_get_display (GimpContext *context)
  908. {
  909.   context_check_current (context);
  910.   context_return_val_if_fail (context, NULL);
  911.  
  912.   return context->display;
  913. }
  914.  
  915. void
  916. gimp_context_set_display (GimpContext *context,
  917.               GDisplay    *display)
  918. {
  919.   context_check_current (context);
  920.   context_return_if_fail (context);
  921.   context_find_defined (context, GIMP_CONTEXT_DISPLAY_MASK);
  922.  
  923.   gimp_context_real_set_display (context, display);
  924. }
  925.  
  926. void
  927. gimp_context_display_changed (GimpContext *context)
  928. {
  929.   context_check_current (context);
  930.   context_return_if_fail (context);
  931.  
  932.   gtk_signal_emit (GTK_OBJECT (context),
  933.            gimp_context_signals[DISPLAY_CHANGED],
  934.            context->display);
  935. }
  936.  
  937. /*  handle dissapearing displays  */
  938. static void
  939. gimp_context_display_destroy (GtkWidget   *disp_shell,
  940.                   GimpContext *context)
  941. {
  942.   context->display = NULL;
  943.   gimp_context_display_changed (context);
  944. }
  945.  
  946. static void
  947. gimp_context_real_set_display (GimpContext *context,
  948.                    GDisplay    *display)
  949. {
  950.   if (context->display == display)
  951.     return;
  952.  
  953.   if (context->display && GTK_IS_OBJECT (context->display->shell))
  954.     gtk_signal_disconnect_by_data (GTK_OBJECT (context->display->shell),
  955.                    context);
  956.  
  957.   if (display)
  958.     gtk_signal_connect (GTK_OBJECT (display->shell), "destroy",
  959.             GTK_SIGNAL_FUNC (gimp_context_display_destroy),
  960.             context);
  961.  
  962.   context->display = display;
  963.  
  964.   /*  set the image _before_ emitting the display_changed signal  */
  965.   if (display)
  966.     gimp_context_real_set_image (context, display->gimage);
  967.  
  968.   gimp_context_display_changed (context);
  969. }
  970.  
  971. static void
  972. gimp_context_copy_display (GimpContext *src,
  973.                GimpContext *dest)
  974. {
  975.   gimp_context_real_set_display (dest, src->display);
  976. }
  977.  
  978. /*****************************************************************************/
  979. /*  tool  ********************************************************************/
  980.  
  981. ToolType
  982. gimp_context_get_tool (GimpContext *context)
  983. {
  984.   context_check_current (context);
  985.   context_return_val_if_fail (context, 0);
  986.  
  987.   return context->tool;
  988. }
  989.  
  990. void
  991. gimp_context_set_tool (GimpContext *context,
  992.                ToolType     tool)
  993. {
  994.   context_check_current (context);
  995.   context_return_if_fail (context);
  996.   context_find_defined (context, GIMP_CONTEXT_TOOL_MASK);
  997.  
  998.   gimp_context_real_set_tool (context, tool);
  999. }
  1000.  
  1001. void
  1002. gimp_context_tool_changed (GimpContext *context)
  1003. {
  1004.   context_check_current (context);
  1005.   context_return_if_fail (context);
  1006.  
  1007.   gtk_signal_emit (GTK_OBJECT (context),
  1008.            gimp_context_signals[TOOL_CHANGED],
  1009.            context->tool);
  1010. }
  1011.  
  1012. static void
  1013. gimp_context_real_set_tool (GimpContext *context,
  1014.                 ToolType     tool)
  1015. {
  1016.   if (context->tool == tool)
  1017.     return;
  1018.  
  1019.   context->tool = tool;
  1020.   gimp_context_tool_changed (context);
  1021. }
  1022.  
  1023. static void
  1024. gimp_context_copy_tool (GimpContext *src,
  1025.             GimpContext *dest)
  1026. {
  1027.   gimp_context_real_set_tool (dest, src->tool);
  1028. }
  1029.  
  1030. /*****************************************************************************/
  1031. /*  foreground color  ********************************************************/
  1032.  
  1033. void
  1034. gimp_context_get_foreground (GimpContext *context,
  1035.                  guchar      *r,
  1036.                  guchar      *g,
  1037.                  guchar      *b)
  1038. {
  1039.   context_check_current (context);
  1040.   context_return_if_fail (context);
  1041.  
  1042.   *r = context->foreground[0];
  1043.   *g = context->foreground[1];
  1044.   *b = context->foreground[2];
  1045. }
  1046.  
  1047. void
  1048. gimp_context_set_foreground (GimpContext *context,
  1049.                  gint         r,
  1050.                  gint         g,
  1051.                  gint         b)
  1052. {
  1053.   context_check_current (context);
  1054.   context_return_if_fail (context);
  1055.   context_find_defined (context, GIMP_CONTEXT_FOREGROUND_MASK);
  1056.  
  1057.   gimp_context_real_set_foreground (context, r, g, b);
  1058. }
  1059.  
  1060. void
  1061. gimp_context_foreground_changed (GimpContext *context)
  1062. {
  1063.   context_check_current (context);
  1064.   context_return_if_fail (context);
  1065.  
  1066.   gtk_signal_emit (GTK_OBJECT (context),
  1067.            gimp_context_signals[FOREGROUND_CHANGED],
  1068.            context->foreground[0],
  1069.            context->foreground[1],
  1070.            context->foreground[2]);
  1071. }
  1072.  
  1073. static void
  1074. gimp_context_real_set_foreground (GimpContext *context,
  1075.                   gint         r,
  1076.                   gint         g,
  1077.                   gint         b)
  1078. {
  1079.   if (context->foreground[0] == r &&
  1080.       context->foreground[1] == g &&
  1081.       context->foreground[2] == b)
  1082.     return;
  1083.  
  1084.   context->foreground[0] = r;
  1085.   context->foreground[1] = g;
  1086.   context->foreground[2] = b;
  1087.  
  1088.   gimp_context_foreground_changed (context);
  1089. }
  1090.  
  1091. static void
  1092. gimp_context_copy_foreground (GimpContext *src,
  1093.                   GimpContext *dest)
  1094. {
  1095.   gimp_context_real_set_foreground (dest,
  1096.                     src->foreground[0],
  1097.                     src->foreground[1],
  1098.                     src->foreground[2]);
  1099. }
  1100.  
  1101. /*****************************************************************************/
  1102. /*  background color  ********************************************************/
  1103.  
  1104. void
  1105. gimp_context_get_background (GimpContext *context,
  1106.                  guchar      *r,
  1107.                  guchar      *g,
  1108.                  guchar      *b)
  1109. {
  1110.   context_check_current (context);
  1111.   context_return_if_fail (context);
  1112.  
  1113.   *r = context->background[0];
  1114.   *g = context->background[1];
  1115.   *b = context->background[2];
  1116. }
  1117.  
  1118. void
  1119. gimp_context_set_background (GimpContext *context,
  1120.                  gint         r,
  1121.                  gint         g,
  1122.                  gint         b)
  1123. {
  1124.   context_check_current (context);
  1125.   context_return_if_fail (context);
  1126.   context_find_defined (context, GIMP_CONTEXT_BACKGROUND_MASK);
  1127.  
  1128.   gimp_context_real_set_background (context, r, g, b);
  1129. }
  1130.  
  1131. void
  1132. gimp_context_background_changed (GimpContext *context)
  1133. {
  1134.   context_check_current (context);
  1135.   context_return_if_fail (context);
  1136.  
  1137.   gtk_signal_emit (GTK_OBJECT (context),
  1138.            gimp_context_signals[BACKGROUND_CHANGED],
  1139.            context->background[0],
  1140.            context->background[1],
  1141.            context->background[2]);
  1142. }
  1143.  
  1144. static void
  1145. gimp_context_real_set_background (GimpContext *context,
  1146.                   gint         r,
  1147.                   gint         g,
  1148.                   gint         b)
  1149. {
  1150.   if (context->background[0] == r &&
  1151.       context->background[1] == g &&
  1152.       context->background[2] == b)
  1153.     return;
  1154.  
  1155.   context->background[0] = r;
  1156.   context->background[1] = g;
  1157.   context->background[2] = b;
  1158.  
  1159.   gimp_context_background_changed (context);
  1160. }
  1161.  
  1162. static void
  1163. gimp_context_copy_background (GimpContext *src,
  1164.                   GimpContext *dest)
  1165. {
  1166.   gimp_context_real_set_background (dest,
  1167.                     src->background[0],
  1168.                     src->background[1],
  1169.                     src->background[2]);
  1170. }
  1171.  
  1172. /*****************************************************************************/
  1173. /*  color utility functions  *************************************************/
  1174.  
  1175. void
  1176. gimp_context_set_default_colors (GimpContext *context)
  1177. {
  1178.   GimpContext *bg_context;
  1179.  
  1180.   context_check_current (context);
  1181.   context_return_if_fail (context);
  1182.  
  1183.   bg_context = context;
  1184.  
  1185.   context_find_defined (context, GIMP_CONTEXT_FOREGROUND_MASK);
  1186.   context_find_defined (bg_context, GIMP_CONTEXT_BACKGROUND_MASK);
  1187.  
  1188.   gimp_context_real_set_foreground (context, 0, 0, 0);
  1189.   gimp_context_real_set_background (bg_context, 255, 255, 255);
  1190. }
  1191.  
  1192. void
  1193. gimp_context_swap_colors (GimpContext *context)
  1194. {
  1195.   GimpContext *bg_context;
  1196.   guchar f_r, f_g, f_b;
  1197.   guchar b_r, b_g, b_b;
  1198.  
  1199.   context_check_current (context);
  1200.   context_return_if_fail (context);
  1201.  
  1202.   bg_context = context;
  1203.  
  1204.   context_find_defined (context, GIMP_CONTEXT_FOREGROUND_MASK);
  1205.   context_find_defined (bg_context, GIMP_CONTEXT_BACKGROUND_MASK);
  1206.  
  1207.   gimp_context_get_foreground (context, &f_r, &f_g, &f_b);
  1208.   gimp_context_get_background (bg_context, &b_r, &b_g, &b_b);
  1209.  
  1210.   gimp_context_real_set_foreground (context, b_r, b_g, b_b);
  1211.   gimp_context_real_set_background (bg_context, f_r, f_g, f_b);
  1212. }
  1213.  
  1214. /*****************************************************************************/
  1215. /*  opacity  *****************************************************************/
  1216.  
  1217. gdouble
  1218. gimp_context_get_opacity (GimpContext *context)
  1219. {
  1220.   context_check_current (context);
  1221.   context_return_val_if_fail (context, 1.0);
  1222.  
  1223.   return context->opacity;
  1224. }
  1225.  
  1226. void
  1227. gimp_context_set_opacity (GimpContext *context,
  1228.               gdouble      opacity)
  1229. {
  1230.   context_check_current (context);
  1231.   context_return_if_fail (context);
  1232.   context_find_defined (context, GIMP_CONTEXT_OPACITY_MASK);
  1233.  
  1234.   gimp_context_real_set_opacity (context, opacity);
  1235. }
  1236.  
  1237. void
  1238. gimp_context_opacity_changed (GimpContext *context)
  1239. {
  1240.   context_check_current (context);
  1241.   context_return_if_fail (context);
  1242.  
  1243.   gtk_signal_emit (GTK_OBJECT (context),
  1244.            gimp_context_signals[OPACITY_CHANGED],
  1245.            context->opacity);
  1246. }
  1247.  
  1248. static void
  1249. gimp_context_real_set_opacity (GimpContext *context,
  1250.                    gdouble      opacity)
  1251. {
  1252.   if (context->opacity == opacity)
  1253.     return;
  1254.  
  1255.   context->opacity = opacity;
  1256.   gimp_context_opacity_changed (context);
  1257. }
  1258.  
  1259. static void
  1260. gimp_context_copy_opacity (GimpContext *src,
  1261.                GimpContext *dest)
  1262. {
  1263.   gimp_context_real_set_opacity (dest, src->opacity);
  1264. }
  1265.  
  1266. /*****************************************************************************/
  1267. /*  paint mode  **************************************************************/
  1268.  
  1269. LayerModeEffects
  1270. gimp_context_get_paint_mode (GimpContext *context)
  1271. {
  1272.   context_check_current (context);
  1273.   context_return_val_if_fail (context, 0);
  1274.  
  1275.   return context->paint_mode;
  1276. }
  1277.  
  1278. void
  1279. gimp_context_set_paint_mode (GimpContext     *context,
  1280.                  LayerModeEffects paint_mode)
  1281. {
  1282.   context_check_current (context);
  1283.   context_return_if_fail (context);
  1284.   context_find_defined (context, GIMP_CONTEXT_PAINT_MODE_MASK);
  1285.  
  1286.   gimp_context_real_set_paint_mode (context, paint_mode);
  1287. }
  1288.  
  1289. void
  1290. gimp_context_paint_mode_changed (GimpContext *context)
  1291. {
  1292.   context_check_current (context);
  1293.   context_return_if_fail (context);
  1294.  
  1295.   gtk_signal_emit (GTK_OBJECT (context),
  1296.            gimp_context_signals[PAINT_MODE_CHANGED],
  1297.            context->paint_mode);
  1298. }
  1299.  
  1300. static void
  1301. gimp_context_real_set_paint_mode (GimpContext     *context,
  1302.                   LayerModeEffects paint_mode)
  1303. {
  1304.   if (context->paint_mode == paint_mode)
  1305.     return;
  1306.  
  1307.   context->paint_mode = paint_mode;
  1308.   gimp_context_paint_mode_changed (context);
  1309. }
  1310.  
  1311. static void
  1312. gimp_context_copy_paint_mode (GimpContext *src,
  1313.                   GimpContext *dest)
  1314. {
  1315.   gimp_context_real_set_paint_mode (dest, src->paint_mode);
  1316. }
  1317.  
  1318. /*****************************************************************************/
  1319. /*  brush  *******************************************************************/
  1320.  
  1321. static GimpBrush *standard_brush = NULL;
  1322.  
  1323. GimpBrush *
  1324. gimp_context_get_brush (GimpContext *context)
  1325. {
  1326.   context_check_current (context);
  1327.   context_return_val_if_fail (context, NULL);
  1328.  
  1329.   return context->brush;
  1330. }
  1331.  
  1332. void
  1333. gimp_context_set_brush (GimpContext *context,
  1334.             GimpBrush   *brush)
  1335. {
  1336.   context_check_current (context);
  1337.   context_return_if_fail (context);
  1338.   context_find_defined (context, GIMP_CONTEXT_BRUSH_MASK);
  1339.  
  1340.   gimp_context_real_set_brush (context, brush);
  1341. }
  1342.  
  1343. void
  1344. gimp_context_brush_changed (GimpContext *context)
  1345. {
  1346.   context_check_current (context);
  1347.   context_return_if_fail (context);
  1348.  
  1349.   gtk_signal_emit (GTK_OBJECT (context),
  1350.            gimp_context_signals[BRUSH_CHANGED],
  1351.            context->brush);
  1352. }
  1353.  
  1354. /*  the active brush was modified  */
  1355. static void
  1356. gimp_context_brush_dirty (GimpBrush   *brush,
  1357.               GimpContext *context)
  1358. {
  1359.   g_free (context->brush_name);
  1360.   context->brush_name = g_strdup (brush->name);
  1361.  
  1362.   gimp_context_brush_changed (context);
  1363. }
  1364.  
  1365. /*  the active brush disappeared  */
  1366. static void
  1367. gimp_context_brush_removed (GimpBrushList *brush_list,
  1368.                 GimpBrush     *brush,
  1369.                 GimpContext   *context)
  1370. {
  1371.   if (brush == context->brush)
  1372.     {
  1373.       context->brush = NULL;
  1374.  
  1375.       gtk_signal_disconnect_by_data (GTK_OBJECT (brush), context);
  1376.       gtk_signal_disconnect_by_data (GTK_OBJECT (brush_list), context);
  1377.       gtk_object_unref (GTK_OBJECT (brush));
  1378.     }
  1379. }
  1380.  
  1381. static void
  1382. gimp_context_real_set_brush (GimpContext *context,
  1383.                  GimpBrush   *brush)
  1384. {
  1385.   if (! standard_brush)
  1386.     standard_brush = brushes_get_standard_brush ();
  1387.  
  1388.   if (context->brush == brush)
  1389.     return;
  1390.  
  1391.   if (context->brush_name && brush != standard_brush)
  1392.     {
  1393.       g_free (context->brush_name);
  1394.       context->brush_name = NULL;
  1395.     }
  1396.  
  1397.   /*  make sure the active brush is swapped before we get a new one...  */
  1398.   if (stingy_memory_use &&
  1399.       context->brush && context->brush->mask &&
  1400.       GTK_OBJECT (context->brush)->ref_count == 2)
  1401.     {
  1402.       temp_buf_swap (brush->mask);
  1403.     }
  1404.  
  1405.   /*  disconnect from the old brush's signals  */
  1406.   if (context->brush)
  1407.     {
  1408.       gtk_signal_disconnect_by_data (GTK_OBJECT (context->brush), context);
  1409.       gtk_object_unref (GTK_OBJECT (context->brush));
  1410.  
  1411.       /*  if we don't get a new brush, also disconnect from the brush list  */
  1412.       if (! brush)
  1413.     {
  1414.       gtk_signal_disconnect_by_data (GTK_OBJECT (brush_list), context);
  1415.     }
  1416.     }
  1417.   /*  if we get a new brush but didn't have one before...  */
  1418.   else if (brush)
  1419.     {
  1420.       /*  ...connect to the brush list  */
  1421.       gtk_signal_connect (GTK_OBJECT (brush_list), "remove",
  1422.               GTK_SIGNAL_FUNC (gimp_context_brush_removed),
  1423.               context);
  1424.     }
  1425.  
  1426.   context->brush = brush;
  1427.  
  1428.   if (brush)
  1429.     {
  1430.       gtk_object_ref (GTK_OBJECT (brush));
  1431.       gtk_signal_connect (GTK_OBJECT (brush), "dirty",
  1432.               GTK_SIGNAL_FUNC (gimp_context_brush_dirty),
  1433.               context);
  1434.       gtk_signal_connect (GTK_OBJECT (brush), "rename",
  1435.               GTK_SIGNAL_FUNC (gimp_context_brush_dirty),
  1436.               context);
  1437.  
  1438.       /*  Make sure the active brush is unswapped... */
  1439.       if (stingy_memory_use &&
  1440.       brush->mask &&
  1441.       GTK_OBJECT (brush)->ref_count < 2)
  1442.     {
  1443.       temp_buf_unswap (brush->mask);
  1444.     }
  1445.  
  1446.       if (brush != standard_brush)
  1447.     context->brush_name = g_strdup (brush->name);
  1448.     }
  1449.  
  1450.   gimp_context_brush_changed (context);
  1451. }
  1452.  
  1453. static void
  1454. gimp_context_copy_brush (GimpContext *src,
  1455.              GimpContext *dest)
  1456. {
  1457.   gimp_context_real_set_brush (dest, src->brush);
  1458.  
  1459.   if ((!src->brush || src->brush == standard_brush) && src->brush_name)
  1460.     {
  1461.       g_free (dest->brush_name);
  1462.       dest->brush_name = g_strdup (src->brush_name);
  1463.     }
  1464. }
  1465.  
  1466. static void
  1467. gimp_context_refresh_brush (GimpContext *context,
  1468.                 gpointer     data)
  1469. {
  1470.   GimpBrush *brush;
  1471.  
  1472.   if (! context->brush_name)
  1473.     context->brush_name = g_strdup (default_brush);
  1474.  
  1475.   if ((brush = gimp_brush_list_get_brush (brush_list, context->brush_name)))
  1476.     {
  1477.       gimp_context_real_set_brush (context, brush);
  1478.       return;
  1479.     }
  1480.  
  1481.   if (gimp_brush_list_length (brush_list))
  1482.     gimp_context_real_set_brush 
  1483.       (context, gimp_brush_list_get_brush_by_index (brush_list, 0));
  1484.   else
  1485.     gimp_context_real_set_brush (context, brushes_get_standard_brush ());
  1486. }
  1487.  
  1488. void
  1489. gimp_context_refresh_brushes (void)
  1490. {
  1491.   g_slist_foreach (context_list, (GFunc) gimp_context_refresh_brush, NULL);
  1492. }
  1493.  
  1494. /*****************************************************************************/
  1495. /*  pattern  *****************************************************************/
  1496.  
  1497. static GPattern *standard_pattern = NULL;
  1498.  
  1499. GPattern *
  1500. gimp_context_get_pattern (GimpContext *context)
  1501. {
  1502.   context_check_current (context);
  1503.   context_return_val_if_fail (context, NULL);
  1504.  
  1505.   return context->pattern;
  1506. }
  1507.  
  1508. void
  1509. gimp_context_set_pattern (GimpContext *context,
  1510.               GPattern    *pattern)
  1511. {
  1512.   context_check_current (context);
  1513.   context_return_if_fail (context);
  1514.   context_find_defined (context, GIMP_CONTEXT_PATTERN_MASK);
  1515.  
  1516.   gimp_context_real_set_pattern (context, pattern);
  1517. }
  1518.  
  1519. void
  1520. gimp_context_pattern_changed (GimpContext *context)
  1521. {
  1522.   context_check_current (context);
  1523.   context_return_if_fail (context);
  1524.  
  1525.   gtk_signal_emit (GTK_OBJECT (context),
  1526.            gimp_context_signals[PATTERN_CHANGED],
  1527.            context->pattern);
  1528. }
  1529.  
  1530. static void
  1531. gimp_context_real_set_pattern (GimpContext *context,
  1532.                    GPattern    *pattern)
  1533. {
  1534.   if (! standard_pattern)
  1535.     standard_pattern = patterns_get_standard_pattern ();
  1536.  
  1537.   if (context->pattern == pattern)
  1538.     return;
  1539.  
  1540.   if (context->pattern_name && pattern != standard_pattern)
  1541.     {
  1542.       g_free (context->pattern_name);
  1543.       context->pattern_name = NULL;
  1544.     }
  1545.  
  1546.   context->pattern = pattern;
  1547.  
  1548.   if (pattern && pattern != standard_pattern)
  1549.     context->pattern_name = g_strdup (pattern->name);
  1550.  
  1551.   gimp_context_pattern_changed (context);
  1552. }
  1553.  
  1554. static void
  1555. gimp_context_copy_pattern (GimpContext *src,
  1556.                GimpContext *dest)
  1557. {
  1558.   gimp_context_real_set_pattern (dest, src->pattern);
  1559.  
  1560.   if ((!src->pattern || src->pattern == standard_pattern) && src->pattern_name)
  1561.     {
  1562.       g_free (dest->pattern_name);
  1563.       dest->pattern_name = g_strdup (src->pattern_name);
  1564.     }
  1565. }
  1566.  
  1567. static void
  1568. gimp_context_refresh_pattern (GimpContext *context,
  1569.                   gpointer     data)
  1570. {
  1571.   GPattern *pattern;
  1572.  
  1573.   if (! context->pattern_name)
  1574.     context->pattern_name = g_strdup (default_pattern);
  1575.  
  1576.   if ((pattern = pattern_list_get_pattern (pattern_list,
  1577.                        context->pattern_name)))
  1578.     {
  1579.       gimp_context_real_set_pattern (context, pattern);
  1580.       return;
  1581.     }
  1582.  
  1583.   if ((pattern = pattern_list_get_pattern_by_index (pattern_list, 0)))
  1584.     gimp_context_real_set_pattern (context, pattern);
  1585.   else
  1586.     gimp_context_real_set_pattern (context, patterns_get_standard_pattern ());
  1587. }
  1588.  
  1589. void
  1590. gimp_context_refresh_patterns (void)
  1591. {
  1592.   g_slist_foreach (context_list, (GFunc) gimp_context_refresh_pattern, NULL);
  1593. }
  1594.  
  1595. static void
  1596. gimp_context_update_pattern (GimpContext *context,
  1597.                  GPattern    *pattern)
  1598. {
  1599.   if (context->pattern == pattern)
  1600.     {
  1601.       if (context->pattern_name)
  1602.     g_free (context->pattern_name);
  1603.  
  1604.       context->pattern_name = g_strdup (pattern->name);
  1605.  
  1606.       gimp_context_pattern_changed (context);
  1607.     }
  1608. }
  1609.  
  1610. void
  1611. gimp_context_update_patterns (GPattern *pattern)
  1612. {
  1613.   g_slist_foreach (context_list, (GFunc) gimp_context_update_pattern, pattern);
  1614. }
  1615.  
  1616. /*****************************************************************************/
  1617. /*  gradient  ****************************************************************/
  1618.  
  1619. static gradient_t *standard_gradient = NULL;
  1620.  
  1621. gradient_t *
  1622. gimp_context_get_gradient (GimpContext *context)
  1623. {
  1624.   context_check_current (context);
  1625.   context_return_val_if_fail (context, NULL);
  1626.  
  1627.   return context->gradient;
  1628. }
  1629.  
  1630. void
  1631. gimp_context_set_gradient (GimpContext *context,
  1632.                gradient_t  *gradient)
  1633. {
  1634.   context_check_current (context);
  1635.   context_return_if_fail (context);
  1636.   context_find_defined (context, GIMP_CONTEXT_GRADIENT_MASK);
  1637.  
  1638.   gimp_context_real_set_gradient (context, gradient);
  1639. }
  1640.  
  1641. void
  1642. gimp_context_gradient_changed (GimpContext *context)
  1643. {
  1644.   context_check_current (context);
  1645.   context_return_if_fail (context);
  1646.  
  1647.   gtk_signal_emit (GTK_OBJECT (context),
  1648.            gimp_context_signals[GRADIENT_CHANGED],
  1649.            context->gradient);
  1650. }
  1651.  
  1652. static void
  1653. gimp_context_real_set_gradient (GimpContext *context,
  1654.                 gradient_t  *gradient)
  1655. {
  1656.   if (! standard_gradient)
  1657.     standard_gradient = gradients_get_standard_gradient ();
  1658.  
  1659.   if (context->gradient == gradient)
  1660.     return;
  1661.  
  1662.   if (context->gradient_name && gradient != standard_gradient)
  1663.     {
  1664.       g_free (context->gradient_name);
  1665.       context->gradient_name = NULL;
  1666.     }
  1667.  
  1668.   context->gradient = gradient;
  1669.  
  1670.   if (gradient && gradient != standard_gradient)
  1671.     context->gradient_name = g_strdup (gradient->name);
  1672.  
  1673.   gimp_context_gradient_changed (context);
  1674. }
  1675.  
  1676. static void
  1677. gimp_context_copy_gradient (GimpContext *src,
  1678.                 GimpContext *dest)
  1679. {
  1680.   gimp_context_real_set_gradient (dest, src->gradient);
  1681.  
  1682.   if ((!src->gradient || src->gradient == standard_gradient) &&
  1683.       src->gradient_name)
  1684.     {
  1685.       g_free (dest->gradient_name);
  1686.       dest->gradient_name = g_strdup (src->gradient_name);
  1687.     }
  1688. }
  1689.  
  1690. static void
  1691. gimp_context_refresh_gradient (GimpContext *context,
  1692.                    gpointer     data)
  1693. {
  1694.   gradient_t *gradient;
  1695.  
  1696.   if (! context->gradient_name)
  1697.     context->gradient_name = g_strdup (default_gradient);
  1698.  
  1699.   if ((gradient = gradient_list_get_gradient (gradients_list,
  1700.                           context->gradient_name)))
  1701.     {
  1702.       gimp_context_real_set_gradient (context, gradient);
  1703.       return;
  1704.     }
  1705.  
  1706.   if (gradients_list)
  1707.     gimp_context_real_set_gradient (context,
  1708.                     (gradient_t *) gradients_list->data);
  1709.   else
  1710.     gimp_context_real_set_gradient (context, gradients_get_standard_gradient ());
  1711. }
  1712.  
  1713. void
  1714. gimp_context_refresh_gradients (void)
  1715. {
  1716.   g_slist_foreach (context_list, (GFunc) gimp_context_refresh_gradient, NULL);
  1717. }
  1718.  
  1719. static void
  1720. gimp_context_update_gradient (GimpContext *context,
  1721.                   gradient_t  *gradient)
  1722. {
  1723.   if (context->gradient == gradient)
  1724.     {
  1725.       if (context->gradient_name)
  1726.     g_free (context->gradient_name);
  1727.  
  1728.       context->gradient_name = g_strdup (gradient->name);
  1729.  
  1730.       gimp_context_gradient_changed (context);
  1731.     }
  1732. }
  1733.  
  1734. void
  1735. gimp_context_update_gradients (gradient_t *gradient)
  1736. {
  1737.   g_slist_foreach (context_list, (GFunc) gimp_context_update_gradient, gradient);
  1738. }
  1739.